﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 坦克大战.Properties;
namespace 坦克大战
{
    internal class GameObjectManager
    {
        private static List<NotMoving> wallList =new List<NotMoving>();
        private static List<NotMoving> steelList = new List<NotMoving>();
        private static List<EnemtTank> enemyTankList = new List<EnemtTank>();
        private static List<Bullet> bulletList = new List<Bullet>();
        private static List<Exp> expList = new List<Exp>();    
        private static NotMoving Boss;
        private static MyTank myTank;
        private static int enemyBornSpeed = 60;
        private static int enemyBornCount = 60;
        private static Point[] points=new Point[3];
        

        public static void Start()
        {
            
            points[0].X = 0; points[0].Y = 0;
            points[1].X = 7*30; points[1].Y = 0;
            points[2].X = 14*30; points[2].Y = 0;
        }
        public static void Update()
        {
            foreach (NotMoving nm in wallList)
            {
                nm.Update();
            }

            foreach (NotMoving nm in steelList)
            {
                nm.Update();
            }
            foreach (EnemtTank tank in enemyTankList)
            {
                tank.Update();

            }

            foreach (Exp exp in expList)
            {
                exp.Update();
            }

            CheckAndDestroyBullet();
            foreach (Bullet bullet in bulletList)
            {
                 bullet.Update();
            }
            CheckAndDestroyExp();

            

            Boss.Update();

            myTank.Update();

            EnemyBorn();

        }

        public static void CreateExp(int x,int y)
        {
            Exp exp = new Exp(x, y);
            expList.Add(exp);
        }

        private static void CheckAndDestroyExp()
        {
            List<Exp> needToDestroy = new List<Exp>();
            foreach (Exp exp in expList)
            {
                if (exp.isDestory == true)
                {
                    needToDestroy.Add(exp);
                }
            }
            foreach (Exp exp in needToDestroy)
            {
                expList.Remove(exp);
            }
        }
        public static void DestoryWall(NotMoving wall)
        {
            wallList.Remove(wall);
        }

        public static void DestoryTank(EnemtTank tank)
        {
            enemyTankList.Remove(tank); 
        }

        private static void CheckAndDestroyBullet()
        {
            List<Bullet> needToDestroy = new List<Bullet>();
            foreach(Bullet bullet in bulletList)
            {
                if(bullet.IsDestroy==true)
                {
                    needToDestroy.Add(bullet);
                }
            }
            foreach (Bullet bullet in needToDestroy)
            {
                bulletList.Remove(bullet);
            }
        }

        public static void CreateBullet(int x,int y,Tag tag,Dirction dir)
        {
            Bullet bullet =new Bullet(x,y,10,dir,tag);
            
                bulletList.Add(bullet);
            




        }


        

        private static void EnemyBorn()
        {
            enemyBornCount++;
            if (enemyBornCount < enemyBornSpeed) return;
            SoundManager.PlayAdd();
            Random rd=new Random();
            int index=rd.Next(3);
            Point postion = points[index];
            int enemyType =rd.Next(5);
            switch (enemyType)
            {
                case 1:
                    CreatEnemyTank1(postion.X, postion.Y);
                    break;
                case 2:
                    CreatEnemyTank2(postion.X, postion.Y);
                    break;
                case 3:
                    CreatEnemyTank3(postion.X, postion.Y);
                    break;
                case 4:
                    CreatEnemyTank4(postion.X, postion.Y);
                    break;




            }



            enemyBornCount = 0;
        }

        public static void CreatEnemyTank1(int x,int y)
        {
            EnemtTank tank = new EnemtTank(x, y, 2, Resources.GrayDown, Resources.GrayUp, Resources.GrayLeft, Resources.GrayRight);
            enemyTankList.Add(tank);
        }
        public static void CreatEnemyTank2(int x, int y)
        {
            EnemtTank tank = new EnemtTank(x, y, 2, Resources.GreenDown, Resources.GreenUp, Resources.GreenLeft, Resources.GreenRight);
            enemyTankList.Add(tank);
        }
        public static void CreatEnemyTank3(int x, int y)
        {
            EnemtTank tank = new EnemtTank(x, y, 4, Resources.QuickDown, Resources.QuickUp, Resources.QuickLeft, Resources.QuickRight);
            enemyTankList.Add(tank);
        }
        public static void CreatEnemyTank4(int x, int y)
        {
            EnemtTank tank = new EnemtTank(x, y, 2, Resources.SlowDown, Resources.SlowUp, Resources.SlowLeft, Resources.SlowRight);
            enemyTankList.Add(tank);
        }


        public static NotMoving IsCollidedWall(Rectangle rectangle)
        {
            foreach(NotMoving wall in wallList)
            {
                if(wall.GetRectangle().IntersectsWith(rectangle))
                {
                    return wall;
                }
            }
            return null;


        }
        public static EnemtTank IsCollidedEnemyTank(Rectangle rectangle)
        {
            foreach(EnemtTank tank in enemyTankList)
            {
                if(tank.GetRectangle().IntersectsWith(rectangle))
                {
                    return tank;
                }
                
            }
            return null;
        }
        public static NotMoving IsCollidedSteel(Rectangle rectangle)
        {
            foreach (NotMoving wall in steelList)
            {
                if (wall.GetRectangle().IntersectsWith(rectangle))
                {
                    return wall;
                }
            }
            return null;


        }

        public static MyTank IsCollidedMyTank(Rectangle rectangle)
        {
            if(myTank.GetRectangle().IntersectsWith(rectangle)) return myTank;
            else
            {
                return null;
            }
        }

        public static  bool IsCollidedBoss(Rectangle rectangle)
        {
            return Boss.GetRectangle().IntersectsWith(rectangle) ;
        }
        public static void DrawMap()
        {
    
        }
        public static void DrawMyTank()
        {
            
        }

        public static void CreateMyTank()
        {
            int x = 5 * 30;
            int y = 14 * 30;
            myTank =new MyTank(x,y,2);

            
        }
        public static void CreateMap()
        {
            CreateWall(1, 1, 5,Resources.wall,wallList);
            CreateWall(3, 1, 5, Resources.wall, wallList);
            CreateWall(5, 1, 4, Resources.wall, wallList);          
            CreateWall(7, 1, 3, Resources.wall, wallList);
            
            CreateWall(9, 1, 4, Resources.wall, wallList);
            CreateWall(11, 1, 5, Resources.wall, wallList);
            CreateWall(13, 1, 5, Resources.wall, wallList);

            CreateWall(7, 5, 1, Resources.steel, steelList);
            
            CreateWall(2, 7, 1, Resources.steel, steelList);
            CreateWall(3, 7, 1, Resources.steel, steelList);
            CreateWall(4, 7, 1, Resources.steel, steelList);

            CreateWall(10, 7, 1, Resources.steel, steelList);
            CreateWall(11, 7, 1, Resources.steel, steelList);
            CreateWall(12, 7, 1, Resources.steel, steelList);

            CreateWall(1, 9, 5, Resources.wall, wallList);
            CreateWall(3, 9, 5, Resources.wall, wallList);
            CreateWall(11, 9, 5, Resources.wall, wallList);
            CreateWall(13, 9, 5, Resources.wall, wallList);

            CreateWall(7, 9, 1, Resources.steel, steelList);

            CreateWall(7, 11, 1, Resources.wall, wallList);
            CreateWall(5, 9, 3, Resources.wall, wallList);
            CreateWall(6, 10, 1, Resources.wall, wallList);
            CreateWall(7, 10, 1, Resources.wall, wallList);
            CreateWall(8, 10, 1, Resources.wall, wallList);
            CreateWall(7, 11, 1, Resources.wall, wallList);
            CreateWall(9, 9, 3, Resources.wall, wallList);
            CreateWall(6, 7, 1, Resources.steel, steelList);
            CreateWall(7, 7, 1, Resources.steel, steelList);
            CreateWall(8, 7, 1, Resources.steel, steelList);

            CreateWall(6, 13, 2, Resources.wall, wallList);
            CreateWall(7, 13, 1, Resources.wall, wallList);
            CreateWall(8, 13, 2, Resources.wall, wallList);
            CreateBoss(7, 14, Resources.Boss);
        }

        private static void CreateBoss(int x, int y, Image img)
        {
            int xPosition = x * 30;
            int yPosition = y * 30;
            Boss = new NotMoving(xPosition, yPosition , img);
            
        }
        private static void CreateWall(int x,int y,int count,Image img, List<NotMoving> wallList) 
        {
            
            int xPosition = x * 30;
            int yPosition = y * 30;
            for (int i = yPosition; i < yPosition + count*30; i+=15)
            {
                NotMoving wall = new NotMoving(xPosition, i, img);
                NotMoving wall1 = new NotMoving(xPosition+15, i, img);
                wallList.Add(wall);
                wallList.Add(wall1);


            }
            
        }

        public static void KeyDown(KeyEventArgs e)
        {
            myTank.KeyDown(e);
        }

        public static void KeyUp(KeyEventArgs e)
        {
            myTank.KeyUp(e);    
        }







    }
}
